package com.qcby.demo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class TransactionProxy implements InvocationHandler {
    private Object target;

    public TransactionProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            System.out.println("开始事务...");
            Object result = method.invoke(target, args);
            System.out.println("提交事务...");
            return result;
        } catch (Exception e) {
            System.out.println("操作失败，回滚事务...");
            throw e; // 这里可以自定义异常处理逻辑
        }
    }
}
